package com.zzh.base

import android.app.Activity
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import com.hammera.common.permission.IPermissionCallBack
import com.hammera.common.permission.PermissionManagerCompat
import java.util.*

/**
 * Created by din on 2018/8/9.
 * <p>
 * Email: godcok@163.com
 *
 */
abstract class BaseActivity : AppCompatActivity(), IPermissionCallBack {
    private val map = hashMapOf<String, String>()
    override fun success(activity: Activity, requestCode: Int) {

    }

    override fun fail(activity: Activity, requestCode: Int, vararg deniedPermissions: String) {
        showSettings(*deniedPermissions)
    }

    override fun alwaysDenied(activity: Activity, requestCode: Int, vararg deniedPermissions: String) {
        showSettings(*deniedPermissions)
    }

    private fun showSettings(vararg deniedPermissions: String) {
        var tips = StringBuffer()
        for (permission in deniedPermissions) {
            if (map.containsKey(permission)) {
                var tip = map[permission]
                if (tips.contains(tip!!, ignoreCase = true)) {
                    continue
                }
                tips.append(map[permission]).append("、")
            }
        }
        if (tips.isNotEmpty()) {
            tips.replace(tips.length - 1, tips.length, "")
        } else {
            return
        }

        val buffer = StringBuffer("在权限中开启")
        buffer.append(tips)
        buffer.append("的权限，不开启权限会导致软件无法正常使用")
        mPermissionManagerCompat?.showRequestPermissionTipsDialog(this, buffer.toString())
    }

    private val fragments = LinkedList<BaseFragment>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initTip()
        initMustData()
        setContentView(getLayoutUI())
        initView()
//        initLoad()
        initData()
    }


    private fun initTip() {
        map[android.Manifest.permission.READ_PHONE_STATE] = "电话"
        map[android.Manifest.permission.WRITE_EXTERNAL_STORAGE] = "存储空间"
        map[android.Manifest.permission.READ_CONTACTS] = "读写通讯录"
        map[android.Manifest.permission.WRITE_CONTACTS] = "读写通讯录"
    }

    abstract fun getLayoutUI(): Int
    abstract fun initData()
    open fun initMustData() {

    }

    //    abstract fun initLoad()
    abstract fun initView()

    protected var mPermissionManagerCompat: PermissionManagerCompat? = null
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        mPermissionManagerCompat?.onRequestPermissionsResult(*permissions,
                grantResults = grantResults)
    }

    fun requestPermission(requestCode: Int, vararg permissions: String) {
        if (mPermissionManagerCompat == null) {
            mPermissionManagerCompat = PermissionManagerCompat.newInstance(this)
        }
        mPermissionManagerCompat
                ?.callback(this)
                ?.permissions(*permissions)
                ?.requestCode(requestCode)
                ?.start()
    }

    fun addFragment(fragment: BaseFragment) {
        addFragment(fragment, R.id.fragment_id)
    }

    fun addFragment(fragment: BaseFragment, @IdRes fragment_id: Int) {
        addFragment(this, fragment, fragment_id)
    }

    fun addFragment(activity: AppCompatActivity, fragment: BaseFragment?, fragment_id: Int) {
        if (fragment == null) {
            return
        }
        fragments.push(fragment)
        var fm = activity.supportFragmentManager
        val ft = fm.beginTransaction()
        if (!fragment.isAdded) {
            ft.add(fragment_id, fragment)
        }
//        ft.addToBackStack(null)
        try {
            ft.commitAllowingStateLoss()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
}